package com.kgc.warehouse.bussness.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Sales;
import com.kgc.warehouse.bussness.entity.Salesback;
import com.kgc.warehouse.bussness.mapper.GoodsMapper;
import com.kgc.warehouse.bussness.mapper.SalesMapper;
import com.kgc.warehouse.bussness.mapper.SalesbackMapper;
import com.kgc.warehouse.bussness.service.SalesbackService;
import com.kgc.warehouse.system.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
* @author main
* @description 针对表【bus_salesback】的数据库操作Service实现
* @createDate 2023-02-04 08:48:32
*/
@Service
public class SalesbackServiceImpl extends ServiceImpl<SalesbackMapper, Salesback> implements SalesbackService{

    @Resource
    private SalesbackMapper salesbackMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private SalesMapper salesMapper;
    @Override
    public void addSalesback(Integer id, Integer number, String remark, HttpSession session) {
        //更新sales表数据
        Sales sales = salesMapper.selectById(id);
        sales.setNumber(sales.getNumber()-number);

        //修改商品的数量
        Goods goods = goodsMapper.selectById(sales.getGoodsid());
        goods.setNumber(goods.getNumber()+number);
        salesMapper.updateById(sales);
        goodsMapper.updateById(goods);

        //添加退货单信息
        Salesback salesback = new Salesback();
        salesback.setCustomerid(sales.getCustomerid());
        salesback.setGoodsid(sales.getGoodsid());
        salesback.setPaytype(sales.getPaytype());
        User user = (User) session.getAttribute("user");
        salesback.setOperateperson(user.getName());
        salesback.setNumber(number);
        salesback.setRemark(remark);
        salesback.setSalesbacktime(new Date());
        salesback.setSalebackprice(sales.getSaleprice());

        salesbackMapper.insert(salesback);


    }

    @Override
    public void deleteSalesback(Integer id) {
        salesbackMapper.deleteById(id);
    }
}




